﻿using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using UserLoging.Dtos;
using UserLoging.Models;
using UserLoging.Services;

namespace UserLoging.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserRolesController : ControllerBase
    {
        public readonly IUserRepository _userRepository;
        public readonly IMapper _mapper;
        public UserRolesController(
            IUserRepository userRepository,
            IMapper mapper
            )
        {
            _userRepository = userRepository;
            _mapper = mapper;
        }
        [HttpOptions]
        public IActionResult Options()
        {
            return Ok();
        }
        [HttpGet]
        [Authorize(Roles = "admin")]
        public async Task<ReturnJson> GetAllUserRoles()
        {
            IEnumerable<User> users = await _userRepository.GetAllUser();
            IEnumerable<UserRolesDto> userRolesDtos = _mapper.Map<IEnumerable<UserRolesDto>>(users);
            if (userRolesDtos.Count() <= 0)
            {
                return new ReturnJson
                {
                    Data = null,
                    Status = 404,
                    Message = "用户获取失败"
                };
            }
            return new ReturnJson
            {
                Data = userRolesDtos,
                Status = 200,
                Message = "用户获取成功"
            };
        }
        [HttpOptions("{userId}")]
        public IActionResult OptionRolesById() 
        {
            return Ok();
        }
        [HttpPut("{userId}")]
        [Authorize(Roles = "admin")]
        public async Task<ReturnJson> demo1([FromRoute]Guid userId,[FromBody]EditUserRoleDto editUserRoleDto)
        {
           HttpRequest request= HttpContext.Request;
            
            if (!(await _userRepository.UserExistsAsync(userId)))
            {
                return new ReturnJson
                {
                    Data = Guid.NewGuid(),
                    Status = 404,
                    Message = "用户不存在"
                };
            }
            User user = await _userRepository.GetUserByUserIdAsync(userId);
            int role = _userRepository.GetPerMissionNumber(editUserRoleDto.Permission);
            if (role == -1)
            {
                return new ReturnJson
                {
                    Data = null,
                    Status = 404,
                    Message = "权限错误"
                };

            }
            user.Permission = (Permission)role;
            _userRepository.UpdataUser(user);
            if (!(await _userRepository.SaveAsync()))
            {
                return new ReturnJson
                {
                    Data = null,
                    Status = 404,
                    Message = "数据储存错误"
                };
            }
            return new ReturnJson
            {
                Data = null,
                Status = 200,
                Message = "修改成功"
            };
        }
    }
}
